<?php
class XDao_ItemI18n extends XDao_Item
{
	// Column which holds default language
	const DEFAULT_LANG = 'default_lang_id';
	
    // language
    protected $langId;

	/**
	 * Stores table schema.
	 *
	 * @return void
	 */
	public function __construct($schema, $langId)
	{
		parent::__construct($schema);
		$this->langId = $langId;
	}

	/**
	 * Loads an item in current object.
	 *
	 * @param   array    associative array with column values
	 * @return  boolean
	 */
	public function load($id)
	{
		$sql = 'SELECT ' . $this->schema->table . '.*';

		if (isset($this->schema->translated_cols) && !empty($this->schema->translated_cols))
		{
			foreach ($this->schema->translated_cols as $key => $col_name)
			{
				$sql .= ', translations_' . $this->langId . '_' . $col_name . '.trans_content AS ' . $col_name;
			}
		}
		
		$sql .= ' FROM ' . $this->schema->table;
		
		if (isset($this->schema->translated_cols) && !empty($this->schema->translated_cols))
		{
			foreach ($this->schema->translated_cols as $key => $col_name)
			{
				$sql .= ' LEFT JOIN translations AS translations_' . $this->langId . '_' . $col_name . ' ON 
					' . $this->schema->table . '.' . $this->schema->columns[$this->schema->primary]->name . ' = translations_' . $this->langId . '_' . $col_name . '.object_id' . '
					AND translations_' . $this->langId . '_' . $col_name . '.lang_id = ' . $this->langId . '
					AND translations_' . $this->langId . '_' . $col_name . '.trans_type = "' . $col_name . '"
				';
			}
		}		
		
		$sql .= ' WHERE ' . $this->schema->table . '.' . $this->schema->columns[$this->schema->primary]->name . ' = ' . 
				$this->schema->db->Escape($id, $this->schema->columns[$this->schema->primary]->type) . '
		';

		// Execute query
		if ($row = $this->schema->db->QueryRow($sql))
		{
			$this->populate($row);
			return TRUE;
		}
		
		return FALSE;
	}

	/**
	 * Deletes a row.
	 *
	 * @return boolean
	 */
	public function delete()
	{
		
		// Primary field must be set
		if (!$this->properties[$this->schema->primary]) return FALSE;

		$sql = '
			DELETE FROM ' . $this->schema->table . '
			WHERE ' . $this->schema->columns[$this->schema->primary]->name . ' = ' . 
				$this->schema->db->Escape($this->properties[$this->schema->primary], $this->schema->columns[$this->schema->primary]->type) . '
		';

		// Execute query
		if (!$this->schema->db->Execute($sql)) return FALSE;
		
		// Delete translations
		if (isset($this->schema->translated_cols) && !empty($this->schema->translated_cols))
		{
			foreach ($this->schema->translated_cols as $col_name)
			{
				$this->delete_translations($col_name, $this->properties[$this->schema->primary]);
			}
		} 
		
		
		// Clears current instance
		$this->reset();
		
		return TRUE;
	}

	/**
	 * Insert / update current values.
	 *
	 * @example $instance = new Model; $object = $instance->fetch(2); $object->save();
	 *
	 * @return boolean
	 */
	public function save()
	{
		// XDao_ItemI18n::DEFAULT_LANG

		// Check if we have changes
		if (0 == array_sum($this->property_changes)) return TRUE;
		
		// Determin whether it's an insert or an update
		$sql = ($this->properties[$this->schema->primary] ? 'UPDATE ' : 'INSERT INTO ') . $this->schema->table . ' SET ';

		// Build query
		$sep = '';
		foreach (array_keys($this->schema->columns) as $column_name)
		{
			if ($this->property_changes[$column_name])
			{
				if (isset($this->schema->translated_cols) && in_array($this->schema->columns[$column_name]->name, $this->schema->translated_cols) 
					&& $this->properties[XDao_ItemI18n::DEFAULT_LANG] != $this->langId)
				{
					continue;
				}
				
				$sql .= $sep . $this->schema->columns[$column_name]->name . ' = ' . 
					$this->schema->db->Escape($this->properties[$column_name], $this->schema->columns[$column_name]->type);
				$sep = ', ';
			}
		}
		
		if ($sep)
		{
			if ($this->properties[$this->schema->primary])
			{
				$sql .= ' WHERE ' . $this->schema->columns[$this->schema->primary]->name . ' = ' . 
					$this->schema->db->Escape($this->properties[$this->schema->primary], $this->schema->columns[$this->schema->primary]->type);
			}

			// Execute query
			if (!$this->schema->db->Execute($sql)) return FALSE;

			// If it's a new row, fetch the last inserted ID
			if (!$this->properties[$this->schema->primary])
			{
				$this->properties[$this->schema->primary] = $this->schema->db->GetInsertId();
			}
		}
		
		if (isset($this->schema->translated_cols) && $this->properties[$this->schema->primary])
		{
			foreach (array_keys($this->schema->columns) as $column_name)
			{
				if ($this->property_changes[$column_name])
				{
					if (isset($this->schema->translated_cols) && in_array($this->schema->columns[$column_name]->name, $this->schema->translated_cols))
					{
						$this->save_translation($this->langId, $this->schema->columns[$column_name]->name, $this->properties[$this->schema->primary], $this->properties[$column_name]);
					}
				}
			}
		}

		// Reset column changes flags
		foreach (array_keys($this->schema->columns) as $column_name)
		{
			$this->property_changes[$column_name] = 0;
		}
		
		return TRUE;
	}
	
	/**
	 * Insert / update a translated value.
	 *
	 * @param   integer  language ID
	 * @param   string   translated value type
	 * @param   integer  object ID
	 * @param   string   translated value
	 * @return  mixed    ID of new translated object or FALSE
	 */
	public function save_translation($lang_id, $trans_type, $object_id, $trans_content)
	{
		$sql = '
			SELECT 
				trans_id
			FROM 
				translations 
			WHERE
				lang_id = ' . intval($lang_id) . '
				AND trans_type = "' . addslashes($trans_type) . '"
				AND object_id = ' . intval($object_id) . '
		';
		
		$trans_id = $this->schema->db->QueryItem($sql);
		
		if ($trans_id)
		{
			$sql = '
				UPDATE translations
				SET
					trans_content = "' . addslashes($trans_content) . '"
				WHERE
					trans_id = ' . intval($trans_id) . '
			';
			if ($this->schema->db->Execute($sql))
			{
				return $trans_id;
			}
		}
		else
		{
			$sql = '
				INSERT INTO translations
				SET
					trans_content = "' . addslashes($trans_content) . '",
					lang_id = ' . intval($lang_id) . ',
					trans_type = "' . addslashes($trans_type) . '",
					object_id = ' . intval($object_id) . '
			';
			if ($this->schema->db->Execute($sql))
			{
				return $this->schema->db->GetInsertId();
			}
		}
		
		return FALSE;
	}

	/**
	 * Delete all translated values for an object.
	 *
	 * @param   string   translated value type
	 * @param   integer  object ID
	 * @return  boolean
	 */
	public function delete_translations($trans_type, $object_id)
	{
		$sql = '
			DELETE FROM translations
			WHERE
				trans_type = "' . addslashes($trans_type) . '"
				AND object_id = ' . intval($object_id) . '
		';
		if ($this->schema->db->Execute($sql))
		{
			return TRUE;
		}
		
		return FALSE;
	}

}
?>